package com.ichina.chat.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.ichina.chat.dao.TrafficDao;
import com.ichina.chat.util.OnlineCounter;
import com.ichina.chat.vo.DataBean;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	@Resource
	private TrafficDao trafficDao;


	public String add() {
		String result = "ok";
		return result;
	}

	public String queryUserTotal() {
		try {
			HttpServletRequest req = ServletActionContext.getRequest();
			// 获得在线人数
			req.setAttribute("onlineTotal",new OnlineCounter().getOnlineSession());
			
	
			
			// 总点击量
			int Totalclick = trafficDao.queryTotalClick();
			req.setAttribute("Totalclick", Totalclick);
			
			// 总独立IP访客量
			int Totalcount = trafficDao.queryUserTotal();
			req.setAttribute("Totalcount", Totalcount);

			// 今天单天访问量
			Date time = new Date();
			int Todaycount = trafficDao.queryUserByToday(time);
			req.setAttribute("Todaycount", Todaycount);

			// 昨天单天访问量
			int yesterdaycount = trafficDao.queryUserByYesterday(time);
			req.setAttribute("yesterdaycount", yesterdaycount);

			// 本周单周总访问量
			int thisWeekcount = trafficDao.queryUserByThisWeek(time);
			req.setAttribute("thisWeekcount", thisWeekcount);

			// 本月单月访问量
			int thisMonthcount = trafficDao.queryUsrByThisMonth(time);
			req.setAttribute("thisMonthcount", thisMonthcount);
			
			// 上周单周总访问量
			int lastWeekcount = trafficDao.queryUserByLastWeek(time);
			req.setAttribute("lastWeekcount", lastWeekcount);

			// 上月单月访问量
			int lastMonthcount = trafficDao.queryUsrByLastMonth(time);
			req.setAttribute("lastMonthcount", lastMonthcount);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "count";
	}
	
	// 关键字排行榜
	public String queryTopkeywords() {
		
		try {
			Map<String,Integer> map = trafficDao.queryTopkeywords();
			this.chartView(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "queryTopkeywords";
	}
	
	// 搜索引擎排行榜
	public String querySearchEngine() throws Exception {
		
			Map<String,Integer> map = trafficDao.querySearchEngine();
			this.chartView(map);
		return "querySearchEngine";
	}
	
	// 接入商排行榜
	public String queryConnectBusiness() {
		
		try {
			Map<String,Integer> map = trafficDao.queryConnectBusiness();
			this.chartView(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "queryConnectBusiness";
	}
	
	// 时间段排行榜
	public String queryVisitDate() {
		
		try {
			Map<String,Integer> map = trafficDao.queryVisitDate();
			this.chartView(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "queryVisitDate";
	}
	
	// 独立IP访问最高日期
	public String queryTopDistinctIpDate() {
		
		HttpServletRequest req = ServletActionContext.getRequest();
		try {
			Map<String,Integer> map = trafficDao.queryTopDistinctIpDate();
			req.setAttribute("topDistinctIpDate", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "topDistinctIpDate";
	}
	
	// 最高浏览量日期
	public String queryTopCountIpDate() {
		
		HttpServletRequest req = ServletActionContext.getRequest();
		try {
			Map<String,Integer> map = trafficDao.queryTopCountIpDate();
			req.setAttribute("topCountIpDate", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "topCountIpDate";
	}
	
	// 统计浏览量日期段
	public String queryChooseDateCountIp() {
		
		HttpServletRequest req = ServletActionContext.getRequest();
		try {
			String start = "2011-04-12";
			String end = "2011-04-18";
			Map<String,Integer> map = trafficDao.queryChooseDateCountIp(start,end);
			req.setAttribute("chooseDateCountIp", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "chooseDateCountIp";
	}
	
	public void chartView(Map<String,Integer> map) throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
			List<DataBean> list = new ArrayList<DataBean> ();
			  for (Map.Entry<String, Integer>  entry : map.entrySet() ) {
				  DataBean data = new DataBean ();
				  data.setName(entry.getKey());
				  data.setViews(entry.getValue());
		            list.add ( data );
			  }
	    response.setContentType ( "text/html;charset=utf-8" );
        request.setCharacterEncoding ( "utf-8" );
        PrintWriter out = response.getWriter ();
        String jsonString = "";
        JSONArray arryData = null;
        arryData = JSONArray.fromObject ( list );
        jsonString = "{totalCount:" + list.size () + ",data:" + arryData.toString ().replaceAll ( "\n", "" ) + "}";
        out.print ( jsonString ); 
        out.flush ();
        out.close ();
	}
}
